package com.moon.game.command.guest.lair;

import java.util.List;

import org.apache.log4j.Logger;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Pet;

/**
 * @description 遗弃宠物
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-23 下午4:22:53
 */
public class AbandonedThePetCmd extends BaseCmd {

	private static final Logger log = Logger.getLogger(AbandonedThePetCmd.class);
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		//是否确认
		Integer sure = Integer.parseInt(ps[1]);
		if (sure == null) {
			doResponsePsError(req);
			return;
		}
		Integer playerPetId = Integer.parseInt(ps[0]);
		PlayerVo player = req.getPlayer();
		List<Pet> pets = player.getPetList();
		Pet thePet = null;
		boolean hasThePet = false;
		if (pets != null) {
			for (Pet pet : pets) {
				if (pet.getPlayerPetId() == playerPetId) {
					hasThePet = true;
					thePet = pet;
					break;
				}
			}
		}
		
		if (!hasThePet) {
			//如果宠物不存在返回错误
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_PET, "你要遗弃的宠物不存在！"));
			return;
		}
		
		if (sure.intValue() == 1) {
			//确认遗弃
			log.debug("remove the pet:" + thePet.getPetId());
			pets.remove(thePet);
			PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
		}else {
			//遗弃前询问
			req.setVO(TAG_ACK, true);
		}
		
		req.setVO("player_pet_id", playerPetId);
		response(req, req.getClientResponse());
	}

}
